package com.loyal.loyalerp.basedata.department.controller;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.ibatis.javassist.Modifier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.loyal.loyalerp.basedata.company.mapper.GyCompanyMapper;
import com.loyal.loyalerp.basedata.department.mapper.DepartmentMapper;
import com.loyal.loyalerp.basedata.department.model.Department;
import com.loyal.loyalerp.basedata.department.service.DepartmentService;
import com.loyal.loyalerp.commons.ServerResponse;
import com.loyal.loyalerp.model.form.GridSearchForm;

@Controller
@RequestMapping("/dept")
public class DepartmentController {
	@Autowired
	DepartmentMapper deptMapper;
	@Autowired
	DepartmentService deptService;
	@Autowired
	GyCompanyMapper companyMapper;
	@Autowired
	EhCacheCacheManager cacheManager;
	
	@RequestMapping("test")
	@ResponseBody
	@Transactional
	public Object test() throws Exception {
		deptMapper.deleteByPrimaryKey(49);
		throw new RuntimeException("test");
	}
	@RequestMapping("cache")
	@ResponseBody
	public Object cache() throws NoSuchMethodException, SecurityException {
		Class<? extends DepartmentMapper> clazz = deptMapper.getClass();
		Method method=clazz.getMethod("selectByPrimaryKey", Integer.class);
		System.out.println(Modifier.isPublic(method.getModifiers()));
		System.out.println(method.getParameters()[0].getName());
		deptService.selectByPrimaryKey(41);
		Cache cache = cacheManager.getCache("idSerializerCache");
		return cache.get("DepartmentServiceImpl_selectByPrimaryKey_41").get();
	}
	
	
	
	@RequestMapping("jsp")
	public String jsp() {
		return "basedata/department/department";
	}
	@RequestMapping("editjsp")
	public String editJsp(ModelMap map) {
		List<Department> depts = new ArrayList<Department>();
		Department rootDept = new Department();
		rootDept.setId(0);
		rootDept.setDeptName("root");
		depts.add(rootDept);
		depts.addAll(deptMapper.selectByExample(null));
		map.put("departments", depts);
		map.put("companys", companyMapper.companyList());
		return "basedata/department/edit";
	}
	@RequestMapping("searchjsp")
	public String searchJsp()
	{
		return "basedata/department/search";
	}
	
	@RequestMapping("list")
	@ResponseBody
	public Object list() {
		return deptService.list();
	}
	@RequestMapping("page")
	@ResponseBody
	public Object page(Integer pageNum,Integer pageSize) {
		try {
			return deptService.page(pageNum, pageSize);
		} catch (Exception e) {
			e.printStackTrace();
			return ServerResponse.createByError("分页出错");
		}
	}
	@RequestMapping("gridSearch")
	@ResponseBody
	public Object search(Boolean _search,GridSearchForm form){
		form.set_search(_search);
		try {
			return deptService.gridSearch(form);
		} catch (Exception e) {
			e.printStackTrace();
			return ServerResponse.createByError("gridSearch出错");
		}
	}
	
	@RequestMapping("gridCUD")	//Create,Update,Delete
	@ResponseBody
	public Object edit(String oper, @Valid Department dept, BindingResult result, HttpServletResponse response)
			throws IOException {
		if (result.hasErrors()) {
			response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
			return ServerResponse.createByError("参数验证失败", result.getAllErrors());
		}
		try {
			deptService.cud(oper, dept);
			return ServerResponse.createBySuccess();
		}catch(Exception e) {
			e.printStackTrace();
			return ServerResponse.createByError(e.getCause().getMessage());
		}
	}

	
}
